import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/public/views/public_scanf.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/views/public_scrollView.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_card.dart';
import '../../views/deliver_order_buttons.dart';
import '../../views/deliver_order_express.dart';
import '../../views/deliver_order_textField.dart';
import 'logic.dart';

class DeliverOrderPage extends StatefulWidget {
  const DeliverOrderPage({Key? key}) : super(key: key);

  @override
  State<DeliverOrderPage> createState() => _DeliverOrderPageState();
}

class _DeliverOrderPageState extends State<DeliverOrderPage> {
  final DeliverOrderLogic logic = Get.put(DeliverOrderLogic());
  FocusNode _orderIDNode = FocusNode();
  FocusNode _phoneNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: MGColors.whileColor,
      appBar: HLAppBar(navigationTitle: "运单扫描"),
      body: HLScrollView(
        nodeList: [_phoneNode, _orderIDNode],
        children: [
          /// 扫一扫
          Container(
            height: 230,
            child: Stack(
              children: [
                HLScanf(
                  scanAreaScale: 1,
                  controller: logic.controller,
                  onCapture: (result) {
                    logic.requestOrderInfo(result);
                  },
                ),
                TextButton(
                    onPressed: () {
                      logic.controller.toggleTorchMode();
                    },
                    child: Row(
                      children: [
                        Image.asset(MGImage.image("deliver_qrlight", type: ImagePathType.deliver), width: 20),
                        SizedBox(width: 10),
                        Text(
                          "请对准面单扫码",
                          style: TextStyle(fontSize: MGFont.textFont3, color: MGColors.whileColor),
                        )
                      ],
                      mainAxisAlignment: MainAxisAlignment.center,
                    ))
              ],
              alignment: AlignmentDirectional.bottomCenter,
            ),
          ),

          /// 快递单号
          MGCard(
            children: [
              DeliverOrderExpress(),
              DeliverOrderTextfield(
                controller: logic.orderIDController,
                node: _orderIDNode,
                hintText: "请扫描或输入快递单号",
              ),
              GetBuilder<DeliverOrderLogic>(builder: (controller) {
                return DeliverOrderTextfield(
                  controller: logic.phoneController,
                  node: _phoneNode,
                  hintText: "请扫描或输入收件号码",
                  enabled: (controller.orderInfoModel.readOnly != null && controller.orderInfoModel.readOnly!) ? false : true,
                );
              }),
              Container(
                margin: EdgeInsets.only(top: 5),
                width: double.infinity,
                child: GetBuilder<DeliverOrderLogic>(
                    init: DeliverOrderLogic(),
                    builder: (controller) {
                      return Text(
                        controller.orderInfoModel.remark ?? "",
                        style: TextStyle(
                          color: MGColors.redColor,
                          fontSize: MGFont.textFont2,
                        ),
                        textAlign: TextAlign.start,
                      );
                    }),
              )
            ],
          )
        ],
      ),
      bottomNavigationBar: DeliverOrderButtons(),
    );
  }

  @override
  void dispose() {
    Get.delete<DeliverOrderLogic>();
    super.dispose();
  }
}
